package com.dyz.leetcode._13_binaryTree;

import com.dyz.leetcode.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class _404_sumOfLeftLeaves {

    //DFS
    int sum = 0;
    public int sumOfLeftLeaves(TreeNode root) {
        preOrder(root, root);
        return sum;
    }

    public void preOrder(TreeNode node, TreeNode parent){
        if(node==null){return;}

        if(node.left==null && node.right==null && parent.left==node){
            sum+=node.val;
        }
        preOrder(node.left, node);
        preOrder(node.right, node);
    }

    //BFS
    public int sumOfLeftLeaves1(TreeNode root) {
        int sum = 0;
        if(root==null){
            return sum;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            TreeNode node = queue.poll();
            if(node.left!=null){
                if(isLeafNode(node.left)){

                }else {
                    queue.add(node.left);
                }
            }
            if(node.right!=null){
                queue.add(node.right);
            }

        }
        return sum;
    }

    private boolean isLeafNode(TreeNode node) {
        return node.left == null && node.right == null;
    }
}
